package rd.hardcore.web
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	import fr.digitas.flowearth.conf.Conf;
	import nochump.util.zip.ZipEntry;
	import nochump.util.zip.ZipFile;
	import rd.hardcore.web.controls.PlayPause;
	import rd.hardcore.web.ui.PicView;
	
	/**
	 * Stand Alone Pad
	 * www.h4rdcore.com
	 * 
	 * @author PRASS
	 */
	public class Main extends Sprite 
	{
		
		private var picview			: PicView;
		private var controls		: PlayPause;
		
		private var hardcoreLoader 	: URLStream;
		
		
		public function Main():void 
		{
			graphics.beginFill(0);
			graphics.drawRect(0, 0, 80, 80);
			graphics.endFill();
			
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Conf.grabParam( loaderInfo );
			
			picview = new PicView();
			addChild(picview);
			
			controls = new PlayPause();
			addChild(controls);
			
			hardcoreLoader = new URLStream();
			hardcoreLoader.addEventListener(Event.COMPLETE, start);
			hardcoreLoader.addEventListener(IOErrorEvent.IO_ERROR, fail);
			hardcoreLoader.load( new URLRequest( Conf.getString("hardcore") ) );
			
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			removeListeners();
			
			hardcoreLoader = null;
			picview = null;
			controls = null;
		}
		
		private function start(e:Event):void 
		{
			removeListeners();
			
			var zipFile:ZipFile = new ZipFile(hardcoreLoader);
			
			for (var i:int = 0; i < zipFile.entries.length; i++)
			{
				var entry:ZipEntry = zipFile.entries[i];
				
				// extract the entry's data from the zip
				var data:ByteArray = zipFile.getInput(entry);
				
				
				/* JPG */
				if ( entry.name.search("jpg") > 0 )
				{
					trace("type: JPG");					
					picview.display( data );
				}
				
				/* MP3 */
				if ( entry.name.search("mp3") > 0 )
				{
					trace("type MP3");
					controls.preload( data );
				}
				
			}
			
						
			addEventListener(MouseEvent.CLICK, 		controls.handleClick);
			addEventListener(PlayPause.START_SOUND, unactive);
			addEventListener(PlayPause.CLOSE_SOUND, active);
			
			active();
			
		}
		
		
		private function active(e:Event = null) : void
		{
			controls.handleOut(null);
			picview.handleOut(null);
			
			addEventListener(MouseEvent.MOUSE_OVER, controls.handleOver);
			addEventListener(MouseEvent.MOUSE_OUT, 	controls.handleOut);
			addEventListener(MouseEvent.MOUSE_OVER, picview.handleOver);
			addEventListener(MouseEvent.MOUSE_OUT, 	picview.handleOut);
		}
		
		
		private function unactive(e: Event = null ) : void
		{	
			removeEventListener(MouseEvent.MOUSE_OVER, controls.handleOver);
			removeEventListener(MouseEvent.MOUSE_OUT, 	controls.handleOut);
			removeEventListener(MouseEvent.MOUSE_OVER, picview.handleOver);
			removeEventListener(MouseEvent.MOUSE_OUT, 	picview.handleOut);
		}
		
		private function removeListeners() : void
		{
			hardcoreLoader.removeEventListener(Event.COMPLETE, start);
			hardcoreLoader.removeEventListener(IOErrorEvent.IO_ERROR, fail);
		}


		
	}
	
}